package com.windaka.elements.config;


import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.redis.connection.RedisConnectionFactory;
import org.springframework.security.oauth2.provider.token.TokenStore;
import org.springframework.security.oauth2.provider.token.store.redis.RedisTokenStore;

import javax.annotation.Resource;

/**
 * Redis存储
 * 配置了将token存储到redis中
 * 配置了存储的前缀
 * 配置了token生成为UUID
 * @author song
 * @date 3:40 下午 2021/11/9
 * @param 
 * @return 
 * @other
**/
@Configuration
public class TokenStoreConfig {

    @Resource
    private RedisConnectionFactory redisConnectionFactory;

    @Value("${token-prefix}")
    private String prefix;

    @Bean
    public TokenStore tokenStore() {
        if (null == prefix || "".equals(prefix)) {
            throw new IllegalArgumentException("配置文件缺少token-prefix");
        }
        RedisTokenStore redisTokenStore = new RedisTokenStore(redisConnectionFactory);
        // 比如 permission: 表示在permission文件夹下 冒号为文件夹
        redisTokenStore.setPrefix(prefix + ":");
        // 设置认证的token生成器 配置成了UUID 不然每次token都一样
        redisTokenStore.setAuthenticationKeyGenerator(new UUIDAuthenticationGenerator());
        return redisTokenStore;
    }
}
